$(function () {

    /*选择器抽取的变量*/
    var vip_datagrid = $("#vip_datagrid");
    var vip_dialog = $("#vip_dialog");
    var vip_form = $("#vip_form");
    var vip_pwddialog = $("#vip_pwddialog");
    var password_form = $("#password_form");


    /*easyui的数据列表组件初始化*/
    vip_datagrid.datagrid({
        title: "员工管理",
        url: "/static/json/emp_datagrid.json",
        fit: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: "#vip_toolbar",
        onSelect: function (index, row) {
            if (row.state) {
                $("#btn_state").linkbutton({
                    text: "离职"
                })
            } else {
                $("#btn_state").linkbutton({
                    text: "复职"
                })
            }
        },
        columns: [[
            {field: 'id', title: 'id', width: 100, checkbox: true},
            {field: 'username', title: '用户名', width: 100},
            {field: 'realname', title: '真实姓名', width: 100},
            {field: 'tel', title: '电话', width: 100, align: 'right'},
            {field: 'email', title: '邮箱', width: 100, align: 'right'},
            {
                field: 'dept', title: '部门', width: 100, align: 'right', formatter: function (value, row, index) {
                    return value ? value.name : '';
                }
            },
            {field: 'hireDate', title: '入职时间', width: 100, align: 'right', sortable: 'true'},
            {
                field: 'state', title: '状态', width: 100, align: 'right', formatter: function (value, row, index) {
                    return value ? "在职" : "<span style='color: red'>离职</span>";
                }
            },
            {
                field: 'admin', title: '管理员', width: 100, align: 'right', formatter: function (value, row, index) {
                    return value ? "是" : "否";
                }
            }
        ]]
    })

    /*弹出编辑框的初始化*/
    vip_dialog.dialog({
        width: 400,
        height: 350,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            vip_form.form('clear');
        }
    })

    /*修改密码弹出框*/
    vip_pwddialog.dialog({
        width: 300,
        height: 115,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["savepassword"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["pwdcancel"]();
            }
        }],
        onClose: function () {
            password_form.form('clear');
        }
    })
    /*导入弹出框*/
    $("#vip_importData").dialog({
        width: 300,
        height: 115,
        closed: true,
        buttons: [{
            text: "上传",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                $("#importData_form").submit();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["importCancel"]();
            }
        }],
        onClose: function () {
            $("input[name='file']").prop("value", "");
        }
    })


    /*事件对象*/
    var methodObj = {
        /*新增员工按钮事件*/
        add: function () {
            vip_dialog.dialog('setTitle', "新增员工");
            $("#tr_password").show();
            vip_dialog.dialog('open');
        },

        /*编辑员工按钮事件*/
        edit: function () {
            vip_dialog.dialog('setTitle', "编辑");
            /*c查看是否有选中的员工*/
            var row = vip_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            $("#tr_password").hide();
            /*数据回显*/
            row["dept.id"] = row.dept.id;
            vip_form.form('load', row);
            /*角色回显,发员工id,查对应的角色*/
            $.get("/role/empRole.do", {id: row.id}, function (data) {
                $("#role_combobox").combobox('setValues', data);
            })

            /*打开弹出框*/
            vip_dialog.dialog('open');
        },

        /*状态改变事件*/
        changeState: function () {
            var row = vip_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "error");
                return;
            }
            $.messager.confirm("温馨提示", '确定要改变状态吗?', function (tr) {
                if (tr) {
                    $.post("/employee/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "question", function () {
                                vip_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error");
                        }
                    })
                }
            })
        },

        /*表单保存事件*/
        save: function () {
            $.messager.confirm("温馨提示", "确定要保存吗?", function () {
                vip_form.form('submit', {
                    url: "/employee/saveOrUpdate.do",
                    onSubmit: function (param) {
                        var values = $("#role_combobox").combobox('getValues');
                        for (var i = 0; i < values.length; i++) {
                            param["roles[" + i + "].id"] = values[i];
                        }
                        /*手动提交校验*/
                        return vip_form.form('validate');
                    },
                    success: function (data) {
                        /*easyui的表单响应数据不是json,需要转换*/
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                vip_dialog.dialog('close');
                                vip_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error")
                        }
                    }
                })
            })
        },

        /*表单取消事件*/
        cancel: function () {
            vip_dialog.dialog('close');
        },

        /*查询事件*/
        query: function () {
            var keyword = $("#keyword").textbox('getValue');
            var beginDate = $("#beginDate").datebox('getValue');
            var endDate = $("#endDate").datebox('getValue');

            vip_datagrid.datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate,
            })
        },
        /*重置密码按钮事件*/
        changePassword: function () {
            var row = vip_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "error")
                return;
            }
            /*填冲id*/
            password_form.form('load', row);
            $.messager.confirm("温馨提示", "确定要需改密码吗", function () {
                vip_pwddialog.dialog('open');
            })
        },

        /*修改密码的保存按钮事件*/
        savepassword: function () {
            $.messager.confirm("温馨提示", "确定要保存吗", function () {
                password_form.form('submit', {
                    url: "/employee/changePassword.do",
                    success: function (data) {
                        /*data是json字符串*/
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("提示", "操作成功!", "info", function () {
                                vip_pwddialog.dialog('close');
                                vip_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("提示", "操作失败", "error");
                        }
                    }
                })
            })
        },
        /*重置密码表单取消事件*/
        pwdcancel: function () {
            vip_pwddialog.dialog('close');
        },

        /*导出事件*/
        export: function () {
            window.open("/employee/export.do?"+$("#vip_queyForm").serialize());
        },

        /*导入事件*/
        importData: function () {
            $("#vip_importData").dialog('open');
        },

        importCancel:function () {
            $("#vip_importData").dialog('close');
        }

    }

    /*统一绑定事件*/
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    })



})
